package functionalTest;
import static org.junit.Assert.assertEquals;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import util.DateUtil;


public class DateUtilTest {
	
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetTimestampFromDDMMYYYYHHMMSS() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		
		assertEquals(vTime, new Timestamp(date.getTime()));
	}

	@Test
	public void testGetCurrentTimestamp() {
		Timestamp vTime = DateUtil.getCurrentTimestamp();
		Date date = new Timestamp(System.currentTimeMillis());
		
		assertEquals(date, vTime);
	}

	@Test
	public void testGetTimestampFromDate() {
		Date date = new Timestamp(System.currentTimeMillis());
		Timestamp vTime = DateUtil.getTimestampFromDate(date);
		
		assertEquals(date, vTime);
	}

	@Test
	public void testGetCurrentDDMMYYYY_CE() {
		String vTime = DateUtil.getCurrentDDMMYYYY_CE();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" + tsStr.substring(0, 4);
		
		assertEquals(tsStr, vTime);
	}

	@Test
	public void testGetCurrentYYYY_CE() {
		String vTime = DateUtil.getCurrentYYYY_CE();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(0, 4);
		
		assertEquals(tsStr, vTime);
	}

	@Test
	public void testGetCurrentDDMMYYYYhh24miss_CE() {
		String vTime = DateUtil.getCurrentDDMMYYYYhh24miss_CE();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" +tsStr.substring(0, 4) + ":" + tsStr.substring(11, 19);
		
		assertEquals(tsStr, vTime);
	}

//	@Test
//	public void testGetStepDDMMYYYY() {
//		fail("Not yet implemented");
//	}

	@Test
	public void testGetCurrentDDMMYYYY_BU() {
		String vTime = DateUtil.getCurrentDDMMYYYY_BU();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" + tsStr.substring(0, 4);
		tsStr = tsStr.substring(0, 6) + (Integer.parseInt(tsStr.substring(6, 10)) + 543);
		
		assertEquals(tsStr, vTime);
	}

	@Test
	public void testGetCurrentDDMMYYYYhh24miss_BU() {
		String vTime = DateUtil.getCurrentDDMMYYYYhh24miss_BU();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(8, 10) + "/" + tsStr.substring(5, 7) + "/" +
			(Integer.parseInt(tsStr.substring(0, 4)) + 543) + ":" + tsStr.substring(11, 19);
		
		assertEquals(tsStr, vTime);
	}

//	@Test
//	public void testGetTimestampFromTimestamString() {
//		fail("Not yet implemented");
//	}

	@Test
	public void testGetTimestampFromDDMMYYYY() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYY("09/12/2010", 0);
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 0, 0, 0);
		Date date = calendar.getTime();
		
		assertEquals(vTime, new Timestamp(date.getTime()));
	}

//	@Test
//	public void testGetTimestampFromCalendar() {
//		fail("Not yet implemented");
//	}

	@Test
	public void testGetDDMMYYYYFromDateDateInt() {
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		String vTime = DateUtil.getDDMMYYYYFromDate(date, 0);
		assertEquals("09/12/2010", vTime);
	}

	@Test
	public void testGetDDMMYYYYFromTimestampTimestampInt() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String result = DateUtil.getDDMMYYYYFromTimestamp(vTime, 0);
		
		assertEquals("09/12/2010", result);
	}

	@Test
	public void testGetDDMMYYYYFromTimestampString() {
		String result = DateUtil.getDDMMYYYYFromTimestampString("2010-12-09:23:59:59", 0);
		
		assertEquals("09/12/2010", result);
	}

//	@Test
//	public void testGetDDMMYYYYhh24missFromTimestamp() {
//		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
//		String result = DateUtil.getDDMMYYYYhh24missFromTimestamp(vTime,0);
//		assertEquals("09/12/2010", result);
//	}

	@Test
	public void testGetDDMMYYYYhh24missFromTimestampString() {
		String vTime = DateUtil.getDDMMYYYYhh24missFromTimestampString("09/12/2010:23:59:59",0);
		assertEquals("09/12/2010:23:59:59", vTime);
	}

	@Test
	public void testGetDDMMYYYYhh24missFromDate() {
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		String vTime = DateUtil.getDDMMYYYYhh24missFromDate(date, true, "");
		assertEquals("09/12/2010:23:59:59", vTime);
	}

	@Test
	public void testGetDDMMYYYYFromDateDateString() {
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		String vTime = DateUtil.getDDMMYYYYFromDate(date, "");
		assertEquals("09/12/2010", vTime);
	}

	@Test
	public void testGetHH24missFromDate() {
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		String vTime = DateUtil.getHH24missFromDate(date, true, "");
		assertEquals("23:59:59", vTime);
	}

	@Test
	public void testGetDiffDays() {
		int duration = DateUtil.getDiffDays("12/12/2010", "14/12/2010");
		assertEquals(2, duration);
	}

	@Test
	public void testGetCurrentYearBU() {
		String vTime = DateUtil.getCurrentYearBU();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = Integer.parseInt(tsStr.substring(0, 4)) + 543+"";
		
		assertEquals(tsStr, vTime);
	}

	@Test
	public void testGetCurrentYearCE() {
		String vTime = DateUtil.getCurrentYearCE();
		String tsStr = new Timestamp(System.currentTimeMillis()).toString();
		tsStr = tsStr.substring(0, 4);
		
		assertEquals(tsStr, vTime);
	}

	@Test
	public void testGetDDMMYYYYFromYYYYMMDDString() {
		String vTime = DateUtil.getDDMMYYYYFromYYYYMMDDString("2010/10/12");
		assertEquals("12/10/2010", vTime);
	}

	@Test
	public void testGetTimeFromYYYYMMDDString() {
		String vTime = DateUtil.getTimeFromYYYYMMDDString("2010/10/12:23:10:00");
		assertEquals("23:10", vTime);
	}

	@Test
	public void testGetYYYYMMDDFromTimestampString() {
		String vTime = DateUtil.getYYYYMMDDFromTimestampString("2010/10/12:23:10:00", 0, "/");
		assertEquals("2010/10/12", vTime);
	}

	@Test
	public void testGetCurrentClockNoSec() {
		String tsStr = new Timestamp(System.currentTimeMillis()).toString().substring(11, 16);
		String clock = DateUtil.getCurrentClockNoSec();
		assertEquals(clock, tsStr);
	}

	@Test
	public void testGetCurrentClock() {
		String tsStr = new Timestamp(System.currentTimeMillis()).toString().substring(11, 19);
		String clock = DateUtil.getCurrentClock();
		assertEquals(clock, tsStr);
	}

	@Test
	public void testGetClockFromDate() {
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 9, 23, 59, 59);
		Date date = calendar.getTime();
		String clock = DateUtil.getClockFromDate(date);
		assertEquals("23:59:59", clock);
	}

	@Test
	public void testGetMMDDYYYYFromDDMMYYYYStringString() {
		String vTime = DateUtil.getMMDDYYYYFromDDMMYYYY("12/10/2010");
		assertEquals("10-12-2010", vTime);
	}

	@Test
	public void testGetMMDDYYYYFromDDMMYYYYString() {
		String vTime = DateUtil.getMMDDYYYYFromDDMMYYYY("12-10-2010","-");
		assertEquals("10-12-2010", vTime);
	}

//	@Test
//	public void testGetFirstDateOfCurrentMonth() {
//		fail("Not yet implemented");
//	}

	@Test
	public void testGetDateNoTime() throws ParseException {
		Date vDate = DateUtil.getDateNoTime("10/12/2010");
		Calendar calendar = new GregorianCalendar(2010,12 - 1, 10, 0, 0, 0);
		Date date = calendar.getTime();
		assertEquals(date.toString(), vDate.toString());
	}

//	@Test
//	public void testGetSqlDateNoTime() {
//		fail("Not yet implemented");
//	}

//	@Test
//	public void testGetDateFromString() throws ParseException {
//		Date vDate = DateUtil.getDateFromString("10/12/2010");
//		Calendar calendar = new GregorianCalendar(2010,12 - 1, 10, 0, 0, 0);
//		Date date = calendar.getTime();
//		assertEquals(date.toString(), vDate.toString());
//	}

	@Test
	public void testGetDDMMYYYYHHMMFromTimestamp() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String vDate = DateUtil.getDDMMYYYYHHMMFromTimestamp(vTime);

		assertEquals("09/12/2010 23:59", vDate.toString());
	}

	@Test
	public void testGetDDMMYYYYFromTimestampTimestamp() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String vDate = DateUtil.getDDMMYYYYFromTimestamp(vTime);

		assertEquals("09/12/2010", vDate.toString());
	}

	@Test
	public void testGetHHMMFromTimestamp() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String vDate = DateUtil.getHHMMFromTimestamp(vTime);

		assertEquals("23:59", vDate.toString());
	}

	@Test
	public void testGetHourMinSecFromTimestamp() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String vDate = DateUtil.getHourMinSecFromTimestamp(vTime);

		assertEquals("23:59:59", vDate.toString());
	}

	@Test
	public void testGetHourMinSecFromTimestampString() {
		String vDate = DateUtil.getHourMinSecFromTimestampString("09/12/2010:23:59:59");

		assertEquals("23:59:59", vDate.toString());
	}

	@Test
	public void testGetHourMinFromTimestamp() {
		Timestamp vTime = DateUtil.getTimestampFromDDMMYYYYHHMMSS("09/12/2010:23:59:59", 0);
		String vDate = DateUtil.getHourMinFromTimestamp(vTime);

		assertEquals("23:59", vDate.toString());
	}

	@Test
	public void testGetHourMinFromTimestampString() {
		String vDate = DateUtil.getHourMinFromTimestampString("09/12/2010:23:59:59");

		assertEquals("23:59", vDate.toString());
	}

}
